import { hub } from "../canvas/hub";
import { Layer } from "../canvas/System/Layer";
import { useToolStore } from "../ToolBox/Tool";
import { useControllerStore } from "../Controller/Controller";

export type userInput = { layer: Layer; data: { x: number; y: number } };
const events = ["drawEnd", "drawing", "drawStart"] as const;
export class User {
    canvasStore;
    controllerStore;
    constructor() {
        this.canvasStore = useToolStore();
        this.controllerStore = useControllerStore();
        this.bindEvent();
        console.log("用户创建成功");
    }
    bindEvent() {
        events.forEach((i) => {
            hub.on(i, this[i]);
        });
    }
    //! 必须要绑定自身
    drawStart = (input: userInput) => {
        this.canvasStore.usingTool.drawStart(input);
    };
    drawEnd = (input: userInput) => {
        this.canvasStore.usingTool.drawEnd(input);
        this.controllerStore.updateLayer(input.layer);
    };
    drawing = (input: userInput) => {
        this.canvasStore.usingTool.drawing(input);
    };
    destroy() {
        events.forEach((i) => hub.off(i as any, (this as any)[i]));
    }
}
